﻿using System;
using System.Collections.Generic;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using Tong;
using JModel;


public partial class aspnet_testPargrameDir_site_Member_Admin_XiaoXiang : System.Web.UI.Page
{
    public string str = "";
    /// <summary>
    /// 當前顯示的id
    /// </summary>
    string id = "1";

    /// <summary>
    /// 用於顯示資料
    /// </summary>
    public Tong.TongUse TextData = new Tong.TongUse();

    /// <summary>
    /// 系统安装绝对路径
    /// </summary>
    public string Abs_Path = TongUse.SysPath;
    /// <summary>
    /// 用户存放数据的临时表
    /// </summary>
    DataTable MyDt = new DataTable();

    public string ho = "";

    /// <summary>
    /// 站点通用方法类
    /// </summary>
    public Tong.ThisSiteUse.SiteTong TextThisSite = new Tong.ThisSiteUse.SiteTong();


    //得到传入的栏目ID
    public String CloumnID = "1";

    /// <summary>
    /// 顯示當前欄目的簡介
    /// </summary>
    public string str1 = "";
    /// <summary>
    /// 資料表
    /// </summary>
    DataTable NewDt = new DataTable();
    /// <summary>
    /// 用于分頁的對像
    /// </summary>
    Tong.Fy.FenYe FY = new Tong.Fy.FenYe();
    /// <summary>
    /// 
    /// </summary>
    public string sud = "";

    /// <summary>
    /// 標題
    /// </summary>
    public string title = "";
    /// <summary>
    /// 表名
    /// </summary>
    public string tab = "Sys_WangZhanDeTue3aae";
    /// <summary>
    /// 学生实体
    /// </summary>
    public CX_Student stud = null;


    /// <summary>
    /// 实休数据操作对像
    /// </summary>
    JModel.DBEntities db = new JModel.DBEntities();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            banji.DataSource = "小熊班,海豚班,小象班,奥尔夫音乐,感觉统合".Split(',');
            banji.DataBind();
            txtdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }

    /// <summary>
    /// 输入学号生成联接
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(stuid.Text))
        {
            TextData.ResonseAlert("请输入会员号");
            return;
        }

        string studid = stuid.Text;
        decimal stid = 0;
        decimal.TryParse(studid, out stid);

        var query = db.CX_Student.Where(p => p.学号 == stid);
        if (query.Count() == 0)
        {
            TextData.ResonseAlert("您输入的会员号不存在,请转到添加学号页面添加");
            return;
        }

        info.NavigateUrl = "show.aspx?stid=" + stid + "&s=" + Server.UrlEncode(banji.SelectedValue) + "&d=" + Server.UrlEncode(txtdate.Text);
        info.Text = "添加" + query.FirstOrDefault().姓名 + " " + txtdate.Text + "教师评语";

        CreateHTMLtable();



    }

    /// <summary>
    /// 内容状态保存时发生
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Page_SaveStateComplete(object sender, EventArgs e)
    {

        db.Connection.Close();
        db.Dispose();
    }


    void CreateHTMLtable()
    {
        AspNetPager1_PageChanged(this, null);
    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, System.EventArgs e)
    {
        string studid = stuid.Text;
        decimal stid = 0;
        decimal.TryParse(studid, out stid);
        var query = db.CX_PingYuIndex.Where(p => p.学号 == stid).OrderByDescending(p => p.评语日期);
        AspNetPager1.RecordCount = query.Count();
        Records.DataSource = query.Skip(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1)).Take(AspNetPager1.PageSize);
        Records.DataBind();
    }
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, System.EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        decimal id = decimal.Parse(btn.ToolTip);
        var dp = db.CX_PingYuIndex.Where(p => p.自动编号 == id).FirstOrDefault();
        if (dp != null)
        {
            db.CX_PingYuIndex.DeleteObject(dp);

            //删除对应的记录 小象班
            var ent = db.CX_Xiaoxiang.Where(p => p.学号 == dp.学号 && p.日期 == dp.评语日期 && p.班级 == dp.班级名称).FirstOrDefault();
            if(ent!=null)
            {
                db.CX_Xiaoxiang.DeleteObject(ent);
            }

            //删除对应的记录 海豚班
            var enthaitun = db.CX_Haitun.Where(p => p.学号 == dp.学号 && p.日期 == dp.评语日期 && p.班级 == dp.班级名称).FirstOrDefault();
            if (enthaitun != null)
            {
                db.CX_Haitun.DeleteObject(enthaitun);
            }

            //删除对应的记录 小熊班
            var entxiaoxiong = db.CX_Xiaoxiong.Where(p => p.学号 == dp.学号 && p.日期 == dp.评语日期 && p.班级 == dp.班级名称).FirstOrDefault();
            if (entxiaoxiong != null)
            {
                db.CX_Xiaoxiong.DeleteObject(entxiaoxiong);
            }
        }

        db.SaveChanges();


        AspNetPager1_PageChanged(this, null);

    }

    /// <summary>
    /// 处理名字
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public string Filter(object name)
    {
        return name.ToString().Replace("'", "&#39;");
    }
}